package com.isoft.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.entity.BeltAssignment;
import com.isoft.mapper.BeltAssignmentMapper;
import com.isoft.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/beltAssignment")
public class BeltAssignmentController {

    @Autowired
    private BeltAssignmentMapper beltAssignmentMapper;

    /**
     * 分页查询行李转盘分配信息
     */
    @GetMapping("/page")
    public R getPage(@RequestParam(defaultValue = "1") Integer current,
                     @RequestParam(defaultValue = "10") Integer size,
                     @RequestParam(required = false) Long flid,
                     @RequestParam(required = false) String ffid,
                     @RequestParam(required = false) String code) {
        Page<BeltAssignment> page = new Page<>(current, size);
        QueryWrapper<BeltAssignment> queryWrapper = new QueryWrapper<>();

        if (flid != null) {
            queryWrapper.eq("flid", flid);
        }
        if (ffid != null && !ffid.isEmpty()) {
            queryWrapper.like("ffid", ffid);
        }
        if (code != null && !code.isEmpty()) {
            queryWrapper.like("code", code);
        }

        IPage<BeltAssignment> pageResult = beltAssignmentMapper.selectPage(page, queryWrapper);
        return R.ok(pageResult);
    }

    /**
     * 获取所有行李转盘分配信息
     */
    @GetMapping("/list")
    public R getList() {
        List<BeltAssignment> list = beltAssignmentMapper.selectList(null);
        return R.ok(list);
    }

    /**
     * 根据ID获取行李转盘分配信息
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        BeltAssignment beltAssignment = beltAssignmentMapper.selectById(id);
        return R.ok(beltAssignment);
    }

    /**
     * 新增行李转盘分配信息
     */
    @PostMapping
    public R save(@RequestBody BeltAssignment beltAssignment) {
        int result = beltAssignmentMapper.insert(beltAssignment);
        return result > 0 ? R.ok("新增成功") : R.error("新增失败");
    }

    /**
     * 更新行李转盘分配信息
     */
    @PutMapping
    public R update(@RequestBody BeltAssignment beltAssignment) {
        int result = beltAssignmentMapper.updateById(beltAssignment);
        return result > 0 ? R.ok("更新成功") : R.error("更新失败");
    }

    /**
     * 删除行李转盘分配信息
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        int result = beltAssignmentMapper.deleteById(id);
        return result > 0 ? R.ok("删除成功") : R.error("删除失败");
    }

    /**
     * 批量删除行李转盘分配信息
     */
    @DeleteMapping("/batch")
    public R deleteBatch(@RequestBody List<Long> ids) {
        int result = beltAssignmentMapper.deleteBatchIds(ids);
        return result > 0 ? R.ok("批量删除成功") : R.error("批量删除失败");
    }
}